package model

import (
	"encoding/json"
)

type PostStatType string

const (
	PostStatLikes    PostStatType = "likes"
	PostStatShares   PostStatType = "shares"
	PostStatComments PostStatType = "comments"
)

// swagger:model Posts
// 帖子,只存储帖子相关内容,用户的内容可以带上
type Posts struct {
	AutoId    `xorm:"extends"`
	UId       `xorm:"extends"`
	TimeModel `xorm:"extends"`

	PostsModifiable   `xorm:"extends"`
	RecommendMeasures `xorm:"extends" json:"-"`
	// 状态,1为可见, 2为隐藏(删除)
	SeeStatus int64 `json:"see_status" xorm:"see_status not null default 0 int"`
	// 内部关联变量, 可忽略
	RelatedGroups []int64 `json:"related_groups" xorm:"related_groups default '' varchar(512)" es:"type:long"`

	// 底下分享,点赞,评论,私信数量
	Shares          int64 `json:"shares" xorm:"shares not null default 0 int"`
	Likes           int64 `json:"likes" xorm:"likes not null default 0 int"`
	Comments        int64 `json:"comments" xorm:"comments not null default 0 int"`
	PrivateMessages int64 `json:"private_messages" xorm:"private_messages not null default 0 int"`
	// 打开, 阅读
	Opens          int64   `json:"opens" xorm:"opens notnull default 0 int"`           // 打开次数
	PartReads      int64   `json:"part_reads" xorm:"part_reads notnull default 0 int"` // 有效阅读, 部分阅读
	AllReads       int64   `json:"all_reads" xorm:"all_reads notnull default 0 int"`   // 完成阅读
	RecommendScore int64 `json:"recommend_score" xorm:"recommend_score int default 0" es:"type:integer"`
}
type RecommendMeasure struct {
	Score  int    `json:"score" es:"type:integer"`
	Weight int    `json:"weight" es:"type:integer"`
	Reason string `json:"reason" es:"type:text"`
}

func NewRecommendMeasure(s string) RecommendMeasure {
	var resp RecommendMeasure
	_ = json.Unmarshal([]byte(s), &resp)
	return resp
}
func (r RecommendMeasure) WeightedScore() int {
	return r.Weight*r.Score
}
func (r RecommendMeasure) String() string {
	b, _ := json.Marshal(r)
	return string(b)
}

type RecommendMeasures struct {
	Placeholder1 string `json:"placeholder1" xorm:"placeholder1 varchar(512) default ''"`
	Placeholder2 string `json:"placeholder2" xorm:"placeholder2 varchar(512) default ''"`
	Placeholder3 string `json:"placeholder3" xorm:"placeholder3 varchar(512) default ''"`
	Placeholder4 string `json:"placeholder4" xorm:"placeholder4 varchar(512) default ''"`
	Placeholder5 string `json:"placeholder5" xorm:"placeholder5 varchar(512) default ''"`
	Placeholder6 string `json:"placeholder6" xorm:"placeholder6 varchar(512) default ''"`
}
type RecommendMeasuresOfEs struct {
	Placeholder1 RecommendMeasure `json:"placeholder1" xorm:"placeholder1 varchar(512) default ''"`
	Placeholder2 RecommendMeasure `json:"placeholder2" xorm:"placeholder2 varchar(512) default ''"`
	Placeholder3 RecommendMeasure `json:"placeholder3" xorm:"placeholder3 varchar(512) default ''"`
	Placeholder4 RecommendMeasure `json:"placeholder4" xorm:"placeholder4 varchar(512) default ''"`
	Placeholder5 RecommendMeasure `json:"placeholder5" xorm:"placeholder5 varchar(512) default ''"`
	Placeholder6 RecommendMeasure `json:"placeholder6" xorm:"placeholder6 varchar(512) default ''"`
}

func (r RecommendMeasuresOfEs) WeightedScore() int64 {
	score := r.Placeholder1.WeightedScore() +
		r.Placeholder2.WeightedScore() +
		r.Placeholder3.WeightedScore() +
		r.Placeholder4.WeightedScore() +
		r.Placeholder5.WeightedScore() +
		r.Placeholder6.WeightedScore()
	return int64(score*20/100)
}

type PostsModifiable struct {
	// 标题
	Title string `json:"title" xorm:"title not null default '' varchar(512)" es:"analyzer:ik_smart"`
	// 正文内容
	Content string `json:"content" xorm:"content not null TEXT"`
	// 地理坐标, "lat,lon"
	Location string `json:"location" xorm:"location not null varchar(128)" es:"type:geo_point,ignore_malformed:true"`
	// 地标地址
	Address string `json:"address" xorm:"address not null varchar(128)"`
	// 封面图
	CoverImage string `json:"cover_image" xorm:"cover_image not null varchar(512)" es:"type:text"`
	// 来自哪个群组,分享到哪几个群组了
	FromGroupId []int64 `json:"from_group_id" xorm:"from_group_id not null varchar(512)" es:"type:long"`
	// 文章类型
	Channel string `json:"channel" xorm:"channel default '' varchar(32)" es:"type:keyword"`
	// 开关, 是否发布到大厅, 1为允许, 2为不允许, 默认为1
	Switch int `json:"switch" xorm:"switch default 0  int" es:"type:long"`
}

// reply状态
const (
	_ int64 = iota
	Post_See_EveryBody
	Post_See_Hide
)
